package com.qiaotongtianxia.loans.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.qiaotongtianxia.loans.R;
import com.qiaotongtianxia.loans.fragments.BaseFragment;
import com.qiaotongtianxia.loans.fragments.LoansFragment;
import com.qiaotongtianxia.loans.fragments.MeFragment;
import com.qiaotongtianxia.loans.fragments.RefundFragment;
import com.qiaotongtianxia.loans.functionInterface.FragmentLogout;
import com.qiaotongtianxia.loans.net.Api;
import com.qiaotongtianxia.loans.util.CommonUtils;
import com.qiaotongtianxia.loans.util.SPUtil;
import com.qiaotongtianxia.loans.util.StatusBarUtils;
import com.qiaotongtianxia.loans.views.BaseTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lisen on 2017/12/14.
 *
 * @author lisen < 4533548588@qq.com >
 */

public class MainActivity extends BaseActivity implements FragmentLogout{
    @BindView(R.id.iv_nav_back)
    ImageView ivNavBack;
    @BindView(R.id.iv_nav_right)
    ImageView iv_nav_right;
    @BindView(R.id.tv_nav_title)
    BaseTextView tvNavTitle;
    @BindView(R.id.content)
    FrameLayout content;
    @BindView(R.id.layout_title)
    RelativeLayout layout_title;

    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;

    private List<BaseFragment> fragments = new ArrayList<>();

    private FragmentManager fragmentManager;
    private BaseFragment loansFragment,refundFragment,meFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void onBaseCreate(@Nullable Bundle savedInstanceState) {
        fragmentManager = getSupportFragmentManager();
        initEvent();
        initLoans();
    }

    @Override
    public Api onApiCreate() {
        return null;
    }

    /**
     * 此处去掉super方法是为了防止fragment的状态被保存，防止app崩溃
     * @param outState
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
    }

    private void initLoans(){
        loansFragment = new LoansFragment();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.content,loansFragment);
        transaction.commit();
        fragments.add(loansFragment);
    }
    private void initEvent() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                iv_nav_right.setVisibility(View.GONE);
                layout_title.setBackgroundColor(ContextCompat.getColor(MainActivity.this,R.color.white));
                tvNavTitle.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.blackText));
                switch (checkedId) {
                    case R.id.rb_loans://贷款
                        showHome();
                        tvNavTitle.setText(R.string.app_name);
                        StatusBarUtils.setWindowStatusBarWhite(MainActivity.this,R.color.mainBg);
                        break;
                    case R.id.rb_refund://还款
                        showRefund();
                        tvNavTitle.setText(R.string.refund);
                        iv_nav_right.setVisibility(View.VISIBLE);
                        StatusBarUtils.setWindowStatusBarWhite(MainActivity.this,R.color.mainBg);
                        break;
                    case R.id.rb_me://我的
                        showMe();
                        tvNavTitle.setText(R.string.center);

                        StatusBarUtils.setWindowStatusBarColor(MainActivity.this,R.color.meTopColor,R.color.mainBg);
                        layout_title.setBackgroundColor(ContextCompat.getColor(MainActivity.this,R.color.meTopColor));
                        tvNavTitle.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.white));
                        break;
                }
            }
        });
    }

    @OnClick({R.id.iv_nav_right})
    public void onViewClick(View view){
        switch (view.getId()){
            case R.id.iv_nav_right:
                Intent intent = new Intent(this,QRCodeActivity.class);
                startActivity(intent);
                break;
        }
    }
    /**
     * 加载贷款
     */
    private void showHome(){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hindAll(transaction);
        if (loansFragment == null){
            loansFragment = new LoansFragment();
            transaction.add(R.id.content,loansFragment);
            fragments.add(loansFragment);
        }else{
            transaction.show(loansFragment);
        }
        transaction.commit();
    }
    /**
     * 加载还款
     */
    private void showRefund(){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hindAll(transaction);
        if (refundFragment == null){
            refundFragment = new RefundFragment();
            transaction.add(R.id.content,refundFragment);
            fragments.add(refundFragment);
        }else{
            transaction.show(refundFragment);
        }
        transaction.commit();
    }
    /**
     * 加载我的
     */
    private void showMe(){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hindAll(transaction);
        if (meFragment == null){
            meFragment = new MeFragment();
            transaction.add(R.id.content,meFragment);
            fragments.add(meFragment);
        }else{
            transaction.show(meFragment);
        }
        transaction.commit();
    }
    private void hindAll(FragmentTransaction transaction){
        for (int i = 0; i < fragments.size(); i++) {
            transaction.hide(fragments.get(i));
        }
    }
    @Override
    public void initTitle() {
        ivNavBack.setVisibility(View.GONE);
        tvNavTitle.setText(R.string.app_name);
    }

    @Override
    public void onFragmentRequestPermission(String[] permissions, int requestCode) {

    }

    @Override
    public void logout() {
        SPUtil.clear(MainActivity.this, SPUtil.ACCOUNT_INFO);
        finish();
    }

    @Override
    public void onBackPressed() {
        if (CommonUtils.doublePressForExit(this)){
            super.onBackPressed();
        }
    }
}
